From 6531835ec125b9c880e2172a0920350dec98b979 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Fri, 13 Oct 2006 04:59:14 +0000 Subject: [PATCH] * API: Added revids parameter. --- includes/api/ApiMain.php | 2 +- includes/api/ApiPageSet.php | 49 ++++++++++++++++++++++++++++-- includes/api/ApiQuery.php | 16 ++++++++++ includes/api/ApiQueryRevisions.php | 5 +-- 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 83949913a8..0b3f6abdc7 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -227,7 +227,7 @@ class ApiMain extends ApiBase { if ( is_a( $e, 'MWException' ) ) { try { $msg = "Exception Caught: {$e->getMessage()}"; - $this->makeErrorMessage($msg, 'internal_error', "\n\n{$e->getTraceAsString()}\n\n"); + $this->makeErrorMessage($msg, 'internal_api_error', "\n\n{$e->getTraceAsString()}\n\n"); $this->printError(); } catch (Exception $e2) { echo $e->__toString(); diff --git a/includes/api/ApiPageSet.php b/includes/api/ApiPageSet.php index 2d4d2cd149..7f3cdc9572 100644 --- a/includes/api/ApiPageSet.php +++ b/includes/api/ApiPageSet.php @@ -34,6 +34,7 @@ class ApiPageSet extends ApiQueryBase { private $mAllPages; // [ns][dbkey] => page_id or 0 when missing private $mGoodTitles, $mMissingTitles, $mMissingPageIDs, $mRedirectTitles, $mNormalizedTitles; private $mResolveRedirects, $mPendingRedirectIDs; + private $mGoodRevIDs, $mMissingRevIDs; private $mRequestedPageFields; @@ -46,6 +47,8 @@ class ApiPageSet extends ApiQueryBase { $this->mMissingPageIDs = array (); $this->mRedirectTitles = array (); $this->mNormalizedTitles = array (); + $this->mGoodRevIDs = array(); + $this->mMissingRevIDs = array(); $this->mRequestedPageFields = array (); $this->mResolveRedirects = $resolveRedirects; @@ -135,16 +138,25 @@ class ApiPageSet extends ApiQueryBase { /** * Get the list of revision IDs (requested with revids= parameter) + * @return array revID (int) => pageID (int) */ public function getRevisionIDs() { - $this->dieUsage(__METHOD__ . ' is not implemented', 'notimplemented'); + return $this->mGoodRevIDs; + } + + /** + * Revision IDs that were not found in the database + * @return array of revision IDs + */ + public function getMissingRevisionIDs() { + return $this->mMissingRevIDs; } /** * Returns the number of revisions (requested with revids= parameter) */ public function getRevisionCount() { - return 0; // TODO: implement + return count($this->getRevisionIDs()); } /** @@ -178,6 +190,8 @@ class ApiPageSet extends ApiQueryBase { $this->initFromPageIds($pageids); break; case 'revids' : + if($this->mResolveRedirects) + $this->dieUsage('revids may not be used with redirect resolution', 'params'); $this->initFromRevIDs($revids); break; default : @@ -348,7 +362,36 @@ class ApiPageSet extends ApiQueryBase { } private function initFromRevIDs($revids) { - $this->dieUsage(__METHOD__ . ' is not implemented', 'notimplemented'); + + $db = $this->getDB(); + $pageids = array(); + $remaining = array_flip($revids); + + $tables = array('page', 'revision'); + $fields = array('rev_id','rev_page'); + $where = array( 'rev_deleted' => 0, 'rev_id' => $revids ); + + // Get pageIDs data from the `page` table + $this->profileDBIn(); + $res = $db->select( $tables, $fields, $where, __METHOD__ ); + while ( $row = $db->fetchObject( $res ) ) { + $revid = intval($row->rev_id); + $pageid = intval($row->rev_page); + $this->mGoodRevIDs[$revid] = $pageid; + $pageids[$pageid] = ''; + unset($remaining[$revid]); + } + $db->freeResult( $res ); + $this->profileDBOut(); + + $this->mMissingRevIDs = array_keys($remaining); + + // Populate all the page information + if($this->mResolveRedirects) + $this->dieDebug('revids may not be used with redirect resolution'); + $pageids = array_keys($pageids); + if(!empty($pageids)) + $this->initFromPageIds($pageids); } private function resolvePendingRedirects() { diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index ea6d35918b..45848b2725 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -195,6 +195,22 @@ class ApiQuery extends ApiBase { $this->getResult()->addValue('query', 'redirects', $redirValues); } + + // + // Missing revision elements + // + $missingRevIDs = $pageSet->getMissingRevisionIDs(); + if (!empty($missingRevIDs)) { + $revids = array(); + foreach ($missingRevIDs as $revid) { + $revids[$revid] = array ( + 'revid' => $revid + ); + } + ApiResult :: setIndexedTagName($revids, 'rev'); + $this->getResult()->addValue('query', 'badrevids', $revids); + } + // // Page elements // diff --git a/includes/api/ApiQueryRevisions.php b/includes/api/ApiQueryRevisions.php index 65e25b6efa..a059ffc566 100644 --- a/includes/api/ApiQueryRevisions.php +++ b/includes/api/ApiQueryRevisions.php @@ -58,13 +58,10 @@ class ApiQueryRevisions extends ApiQueryBase { if ($revCount === 0 && $pageCount === 0) return; - if ($revCount > 0 && $pageCount > 0) - $this->dieUsage('The revids= parameter may not be used with titles, pageids, or generator options.', 'revids'); - if ($revCount > 0 && $enumRevMode) $this->dieUsage('The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids'); - if ($revCount === 0 && $pageCount > 1 && $enumRevMode) + if ($pageCount > 1 && $enumRevMode) $this->dieUsage('titles, pageids or a generator was used to supply multiple pages, but the limit, startid, endid, dirNewer, start, and end parameters may only be used on a single page.', 'multpages'); $tables = array ( -- 2.20.1